<template>
  <div class="qy pad_20">
    <el-form :model="formData" label-width="120px">
      <el-form-item label="周期返利标题" prop="cycleTitle" :required="true">
        <el-input type="text" v-model="formData.cycleTitle" placeholder="请输入周期返利标题"></el-input>
      </el-form-item>
      <el-form-item label="周期返利副标题" prop="cycleSubtitle" :required="true">
        <el-input type="text" v-model="formData.cycleSubtitle" placeholder="请输入周期返利副标题"></el-input>
      </el-form-item>
      <el-form-item label="周期返利" label-width="100px">
        <el-checkbox-group v-model="open" @change="change">
          <el-checkbox label="1">启用</el-checkbox>
        </el-checkbox-group>
        <template v-if="open.length">
          <div class="flex mar_b15">
            <span>购卡后每隔</span>
            <el-input class="mar_lr20" style="width: 150px;" v-model="formData.day">
              <template slot="append">天</template>
            </el-input>
            <span>可以送出以下礼品</span>
          </div>
          <el-form-item label="购卡送优惠券" label-width="100px">
            <el-select v-model="formData.couponArr" multiple default-first-option placeholder="请选择优惠券">
              <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </template>
      </el-form-item>
    </el-form>
    <div class="mar_t20 t_c">
      <el-button type="primary" @click="submitForm">保存</el-button>
    </div>
  </div>
</template>

<script>
import { vip_couponList } from '@/api/plug';
import { getConfig, postConfig } from '@/api/setup.js';
export default {
  data() {
    return {
      formData: {
        ident: 'payVipCycleRebate',
        cycleTitle: '',
        cycleSubtitle: '',
        open: '2',
        day: '1',
        couponArr: []
      },
      open: [],
      options: []
    };
  },
  created() {
    vip_couponList().then((res) => {
      if (res.code == 1) {
        this.options = res.data;
        this.getConfigPay();
      }
    });
  },
  methods: {
    getConfigPay() {
      getConfig({
        ident: 'payVipCycleRebate'
      }).then((res) => {
        Object.assign(this.formData, res.data);
        if (res.data.open == 1) {
          this.open = ['1'];
        } else {
          this.open = [];
        }
      });
    },
    change(msg) {
      if (msg.length) {
        this.formData.open = 1;
      } else {
        this.formData.open = 2;
      }
    },
    submitForm() {
      postConfig(this.formData).then((res) => {
        if (res.code == 1) {
          this.$baseMessage('保存成功！', 'success');
        }
      });
    }
  }
};
</script>
